<div class="section" id="sitemaplist">

	<div class="section-title"><?php echo $this->document->title?></div>
    <form id="frm_recipt_process">
    <div class="section-content padding1">
    		<div class="button right">
            	<input type="button" value="Lưu" class="button" onClick="save('')"/>
                <input type="button" value="Lưu & In" class="button" onClick="save('print')"/>
     	        <input type="button" value="Bỏ qua" class="button" onclick="linkto('?route=gara/receipt')"/>   
     	        <input type="hidden" name="id" value="<?php echo $item['id']?>">
                
                
                
            </div>
    <table>
        <tr>
            <td width="50%">
            	<p>
                	<label>Tên khách hàng</label>
                    <?php echo $item['customername']?>
                </p>
                <p>
                	<label>Số điện thoại</label>
                    <?php echo $item['phone']?>
                </p>
                <p>
                	<label>Email</label>
                    <?php echo $item['email']?>
                </p>
                <p>
                	<label>Địa chỉ</label>
                    <?php echo $item['address']?>
                </p>
            </td>
            <td>
            	<p>
                	<label>Số xe</label>
                    <?php echo $item['carnumber']?>
                </p>
                <p>
                	<label>Hảng xe</label>
                    <?php echo $this->document->getCategory($item['manufacturer'])?>
                </p>
                <p>
                	<label>Dòng xe</label>
                    <?php echo $item['model']?>
                </p>
                <p>
                	<label>Màu</label>
                    <?php echo $item['color']?>
                </p>
                <p>
                	<label>Mô tả</label>
                    <?php echo $item['description']?>
                </p>
            </td>
        </tr>
        <tr>
        	<td colspan="2">
            	<div id="fragment-nguyenlieu">
                	
                	<table>
                    	<thead>
                            <tr>
                                <th>Code</th>
                                <th>Sản phẩm</th>
                                <th>Số lượng</th>
                                <th>Đơn vị tính</th>
                                <th>Đơn giá</th>
                                <th>Giảm giá%</th>
                                <th>Giảm giá</th>
                                
                                <th>Thành tiền</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody id="nhapkhonguyenlieu">
                        </tbody>
                        <tfoot>
                        	<tr>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td>
                                	
                                    <input type="text" id="lydothu" name="lydothu" class="text" value="<?php echo $item['feename']?>"/>
                                </td>
                                <td class="number"><input type="text" class="text number"  id="thuphi" name="thuphi" value="<?php echo $this->string->numberFormate($item['fee'])?>"/></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td></td>
                                <td class="number">Tổng số lượng</td>
                                <td id="sumsoluong" class="number"></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td class="number">
                                	Tổng cộng
                                    <input type="hidden" id="tongtien" name="tongtien" value="<?php echo $item['total']?>"/>
                                </td>
                                <td class="number" id="tongcong"><?php echo $this->string->numberFormate($item['total'])?></td>
                                <td></td>
                            </tr>
                            
                            <tr>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td class="number">Thanh toán</td>
                                <td class="number"><input type="text" class="text number"  id="thanhtoan" name="thanhtoan" value="<?php echo $this->string->numberFormate($item['payment'])?>"/></td>
                                <td><input type="button" class="button" id="btnTrahet" value="Trả hết"/></td>
                            </tr>
                            <tr>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td class="number">
                                	Công nợ
                                	<input type="hidden" id="congno" name="congno" value="<?php echo $item['remain']?>"/>
                                </td>
                                <td class="number" id="lbl-congno"><?php echo $this->string->numberFormate($item['remain'])?></td>
                                <td></td>
                            </tr>
                            <!--<tr>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td class="number">Số ngày công nợ</td>
                                <td class="number"><input type="text" class="text number"  id="songaycongno" name="songaycongno" value="<?php echo $this->string->numberFormate($item['songaycongno'])?>"/></td>
                                <td>Ngày</td>
                            </tr>-->
                        </tfoot>
                    </table>
                    <input type="hidden" id="delnhapkho" name="delnhapkho" />
                    <input type="button" class="button" id="btnAddRow" value="Thêm dòng"/>
                </div>
            </td>
        </tr>
    </table>
    </div>
    </form>
</div>
<?php if(count($data_detail)){ ?>
	<?php foreach($data_detail as $dl){ ?>
<script language="javascript">
$(document).ready(function(e) {
	objdl.addRow("<?php echo $dl['id']?>","<?php echo $dl['mediaid']?>","","<?php echo $dl['ProductName']?>","<?php echo $dl['quantity']?>","<?php echo $dl['unit']?>","<?php echo $dl['price']?>","<?php echo $dl['discount']?>","<?php echo $dl['discountpercent']?>");
});
	
	
</script>
	<?php }?>
<?php }?>
<script language="javascript">
$('#btnTrahet').click(function(e) {
    $('#thanhtoan').val($('#tongcong').html());
	$('#thanhtoan').keyup();
});
$('#thuphi').keyup(function(e) {
    objdl.tinhtong(0);
});
$('#thanhtoan').keyup(function(e) {
    var tongcong = Number(stringtoNumber($('#tongcong').html()));
	var thanhtoan = Number(stringtoNumber($('#thanhtoan').val()));
	var congno = tongcong - thanhtoan;
	$('#congno').val(congno);
	$('#lbl-congno').html(formateNumber(congno));
});
$('#btnAddRow').click(function(e) {
	browseProduct();
});

function save(type)
{
	$.blockUI({ message: "<h1>Please wait...</h1>" }); 
	
	$.post("?route=gara/receipt/saveDetail", $("#frm_recipt_process").serialize(),
		function(data){
			var arr = data.split("-");
			if(arr[0] == "true")
			{
				switch(type)
				{
					case "":
						window.location = "?route=gara/receipt";
						break;
					case "print":
						$.unblockUI();
						var id = arr[1];
						$("#popup").attr('title','Phiếu nhập kho');
						$( "#popup" ).dialog({
							autoOpen: false,
							show: "blind",
							hide: "explode",
							width: 900,
							height: 600,
							modal: true,
							close: function(ev, ui){
									window.location = "?route=gara/receipt";
								},
							buttons: {
								
								'In':function()
								{
									openDialog("?route=gara/receipt/view&id="+id+"&opendialog=print",800,500)
									window.location = "?route=gara/receipt";
								},
								'Đóng': function() 
								{
									
									$( this ).dialog( "close" );
									window.location = "?route=quanlykho/phieuxuat";
								},
							}
						});
					
						
						$("#popup-content").load("?route=gara/receipt/view&id="+id+"&opendialog=true",function(){
							$("#popup").dialog("open");
						});
				}
			}
			else
			{
			
				$('#error').html(data).show('slow');
				$.unblockUI();
				
			}
			
		}
	);
}
</script>
